clear

use "~\Datasets to combine\CCAnoms58to20matched"
gen gen_matched=1
destring, replace ignore("NA")
tostring nomabamin2, replace
sort nomid 
merge 1:1 nomid using "~\Datasets to combine\CCAnoms1958to2020", generate (_merge0)
merge m:m scfjid using "~\Datasets to combine\abascfjmembers", generate (_merge1) keep(match)
merge m:m commyear using "~\Datasets to combine\ABAyearsplits", generate (_merge3) keep(match)
drop if commyear<1958
replace nomfedjudyrs=0 if nomid=="BC73"
replace nomstjudyrs=0 if nomid=="BC73"
replace nomallpol=1 if nomid=="W23"
replace nomsplitvote=0 if nomsplitvote==.
replace nomabamajid=2 if nomname=="Bane, Charles"
gen scfjparty=0 if scfjna==1
replace scfjparty=1 if scfjrepublican==1
replace scfjparty=2 if scfjdemocrat==1
gen scfjparty2=.
replace scfjparty2=0 if scfjdemocrat==1
replace scfjparty2=1 if scfjna==1
replace scfjparty2=2 if scfjrepublican==1
gen scfjparty3=.
replace scfjparty3=0 if scfjdemocrat==1
replace scfjparty3=1 if scfjna==1 | scfjrepublican==1
gen scfjrepdem=0 if scfjdemocrat==1
replace scfjrepdem=1 if scfjrepublican==1
gen commrepdem=.
replace commrepdem=commrepublican/(commrepublican+commdemocrat)
gen commconservative=.
replace commconservative=(commrepublican+commnapartisan)/(commrepublican+commdemocrat+commnapartisan)
gen commliberal=.
replace commliberal=1-commconservative
gen nomnosplitvoteera=1 if commyear<1978
replace nomnosplitvoteera=0 if nomnosplitvoteera==.
replace nomabarating="Q/WQ/NQ" if nomname=="Colloton, Steven"
replace nomabaratingid=2 if nomname=="Colloton, Steven"
replace nomabaratingid=5 if nomname=="Sachs, Howard" | nomname=="Nickerson, Eugene"
replace nomabaratingid=5 if nomname=="Walker, Justin R."
save "~\masterset", replace
clear

use "~\masterset"
expand 2, gen(dup2)
replace nomabaratingid=. if dup2==1
replace scfjparty3=1 if dup2==1
replace commconservative=0.647 if dup2==1
replace nomgender=0 if dup2==1
replace nomrace=0 if dup2==1
replace nomnosplitvoteera=0 if dup2==1
ologit nomabaratingid c.nomfedjudyrs c.nomstjudyrs c.nomprivcorppiyrs c.nomgovattyrs i.nomt14 i.nomlawprof i.nomfedclerk i.nomallpol i.nomprezparty##i.scfjparty3##c.commconservative i.nomrace i.nomgender c.nomage##c.nomage i.nomnosplitvoteera if gen_matched==1 & nomrenom==0, vce(cl commyear)
predict nompredababetter, xb
gen roundaba=round(nompredababetter, .1)
ologit nomabaratingid c.nomfedjudyrs c.nomstjudyrs c.nomprivcorppiyrs c.nomgovattyrs i.nomt14 i.nomlawprof i.nomfedclerk i.nomallpol i.nomprezparty##i.scfjparty3##c.commconservative i.nomrace i.nomgender c.nomage##c.nomage i.nomnosplitvoteera if gen_matched==1 & nomrenom==0, vce(cl commyear)
predict nompredababettererror, stdp
keep if dup2==1
save "~\betteradjustment", replace
clear

use "~\masterset"
merge 1:1 nomid using "~\betteradjustment", generate(_merge5)
egen double stdnompredababetter=std(nompredababetter), mean(0) std(1)
corr nomabaratingid nomprezparty
corr stdnompredababetter nomprezparty
corr nomabaratingid nomfedjudyrs
corr stdnompredababetter nomfedjudyrs
corr nomabaratingid nomstjudyrs
corr stdnompredababetter nomstjudyrs
corr nomabaratingid nomprivcorppiyrs
corr stdnompredababetter nomprivcorppiyrs
corr nomabaratingid nomgovattyrs
corr stdnompredababetter nomgovattyrs
corr nomabaratingid nomt14
corr stdnompredababetter nomt14
corr nomabaratingid nomlawprof
corr stdnompredababetter nomlawprof
corr nomabaratingid nomfedclerk
corr stdnompredababetter nomfedclerk
corr nomabaratingid nomallpol
corr stdnompredababetter nomallpol
///The next two lines create Figure 1
graph bar (count) if nomrenom==0, over(nomabaratingid) 
histogram stdnompredababetter if nomrenom==0, frequency kdensity 
summarize nomabaratingid if nomrenom==0, detail
summarize stdnompredababetter if nomrenom==0, detail
corr nomabaratingid stdnompredababetter if nomrenom==0

expand 2 if nomid=="W28" | nomid=="W52" | nomid=="O1", gen(hearing2) 
replace hearing2=1 if nomid=="W82" | nomid=="W83" | nomid=="W84" | nomid=="W86" | nomid=="W50" | nomid=="BC44" | nomid=="BC46" | nomid=="BC49" | nomid=="BC73" | nomid=="O33"
sort nomid
expand 20, gen(hearingid)
replace hearingid=. if hearingid==1
replace hearingid=1 if hearingid==0
sort nomid hearing2 hearingid
replace hearingid = hearingid[_n-1]+1 if missing(hearingid)
egen confirmationid = concat(hearing2 hearingid nomid)
save "~\qualitymeasurementmaster.dta", replace

clear

use "~\Datasets to combine\confirmationhearings.dta"
replace presenter=0 if presenter==.
replace presiding=0 if presiding==.
replace nomineespeaker=0 if nomineespeaker==.
replace housespeaker=0 if housespeaker==.
replace otherspeaker=0 if otherspeaker==.
gen speakerid=.
replace speakerid=0 if senatorspeaker==1
replace speakerid=1 if nomineespeaker==1
replace speakerid=2 if housespeaker==1
replace speakerid=3 if otherspeaker==1
drop confirmationid
egen confirmationid = concat(hearing2 hearingid1 nomid)

gen speakerscore=.
replace speakerscore=0.388 if speaker=="Chairman HATCH." | speaker=="Senator HATCH."
replace speakerscore=-0.386 if speaker=="Chairman LEAHY." | speaker=="Senator LEAHY."
replace speakerscore=0.061 if speaker=="Chairman SPECTER." | speaker=="Senator SPECTER."
replace speakerscore=-0.333 if speaker=="Senator SPECTER." & year>=2009
replace speakerscore=0.314 if speaker=="Senator ABRAHAM."
replace speakerscore=-0.423 if speaker=="Senator AKAKA."
replace speakerscore=0.324 if speaker=="Senator ALEXANDER."
replace speakerscore=0.586 if speaker=="Senator ALLARD."
replace speakerscore=0.417 if speaker=="Senator ALLEN."
replace speakerscore=0.551 if speaker=="Senator ASHCROFT."
replace speakerscore=-0.174 if speaker=="Senator BAYH."
replace speakerscore=-0.257 if speaker=="Senator BEGICH."
replace speakerscore=0.33 if speaker=="Senator BENNETT."
replace speakerscore=-0.321 if speaker=="Senator BIDEN."
replace speakerscore=-0.196 if speaker=="Senator BILL NELSON."
replace speakerscore=-0.281 if speaker=="Senator BINGAMAN."
replace speakerscore=-0.417 if speaker=="Senator BLUMENTHAL."
replace speakerscore=0.638 if speaker=="Senator BOB SMITH."
replace speakerscore=0.307 if speaker=="Senator BOND."
replace speakerscore=-0.465 if speaker=="Senator BOXER."
replace speakerscore=-0.118 if speaker=="Senator BREAUX."
replace speakerscore=-0.452 if speaker=="Senator BROWN."
replace speakerscore=0.466 if speaker=="Senator BROWNBACK."
replace speakerscore=-0.258 if speaker=="Senator BRYAN."
replace speakerscore=0.529 if speaker=="Senator BUNNING."
replace speakerscore=0.469 if speaker=="Senator BURR."
replace speakerscore=-0.298 if speaker=="Senator BYRD."
replace speakerscore=0.231 if speaker=="Senator CAMPBELL."
replace speakerscore=-0.301 if speaker=="Senator CANTWELL."
replace speakerscore=-0.337 if speaker=="Senator CARDIN."
replace speakerscore=-0.182 if speaker=="Senator CARPER."
replace speakerscore=-0.308 if speaker=="Senator CASEY."
replace speakerscore=0.435 if speaker=="Senator CHAMBLISS."
replace speakerscore=-0.232 if speaker=="Senator CLELAND."
replace speakerscore=-0.381 if speaker=="Senator CLINTON."
replace speakerscore=0.807 if speaker=="Senator COBURN."
replace speakerscore=0.291 if speaker=="Senator COCHRAN."
replace speakerscore=0.179 if speaker=="Senator COLEMAN."
replace speakerscore=0.088 if speaker=="Senator COLLINS."
replace speakerscore=-0.317 if speaker=="Senator CONRAD."
replace speakerscore=-0.314 if speaker=="Senator COONS."
replace speakerscore=0.378 if speaker=="Senator CORKER."
replace speakerscore=0.517 if speaker=="Senator CORNYN."
replace speakerscore=-0.469 if speaker=="Senator CORZINE."
replace speakerscore=0.418 if speaker=="Senator COVERDELL."
replace speakerscore=0.5 if speaker=="Senator CRAIG."
replace speakerscore=0.501 if speaker=="Senator CRAPO."
replace speakerscore=0.179 if speaker=="Senator DAMATO."
replace speakerscore=0.288 if speaker=="Senator DEWINE."
replace speakerscore=-0.222 if speaker=="Senator DODD."
replace speakerscore=0.373 if speaker=="Senator DOLE."
replace speakerscore=0.259 if speaker=="Senator DOMENICI."
replace speakerscore=-0.288 if speaker=="Senator DORGAN."
replace speakerscore=-0.375 if speaker=="Senator DURBIN."
replace speakerscore=-0.304 if speaker=="Senator EDWARDS."
replace speakerscore=0.533 if speaker=="Senator ENSIGN."
replace speakerscore=0.552 if speaker=="Senator ENZI."
replace speakerscore=-0.406 if speaker=="Senator FEINGOLD."
replace speakerscore=-0.279 if speaker=="Senator FEINSTEIN."
replace speakerscore=0.276 if speaker=="Senator FITZGERALD."
replace speakerscore=-0.433 if speaker=="Senator FRANKEN."
replace speakerscore=0.37 if speaker=="Senator FRIST."
replace speakerscore=-0.297 if speaker=="Senator GILLIBRAND."
replace speakerscore=0.192 if speaker=="Senator GORDON SMITH."
replace speakerscore=0.268 if speaker=="Senator GORTON."
replace speakerscore=0.422 if speaker=="Senator GRAHAM."
replace speakerscore=0.559 if speaker=="Senator GRAMM."
replace speakerscore=0.343 if speaker=="Senator GRASSLEY."
replace speakerscore=0.429 if speaker=="Senator GREGG."
replace speakerscore=-0.213 if speaker=="Senator HAGAN."
replace speakerscore=0.341 if speaker=="Senator HAGEL."
replace speakerscore=-0.372 if speaker=="Senator HARKIN."
replace speakerscore=-0.286 if speaker=="Senator HOLLINGS."
replace speakerscore=0.429 if speaker=="Senator HUTCHINSON."
replace speakerscore=0.358 if speaker=="Senator HUTCHISON."
replace speakerscore=0.556 if speaker=="Senator INHOFE."
replace speakerscore=0.416 if speaker=="Senator ISAKSON."
replace speakerscore=-0.298 if speaker=="Senator JEFFORDS."
replace speakerscore=-0.41 if speaker=="Senator KAUFMAN."
replace speakerscore=-0.46 if speaker=="Senator KENNEDY."
replace speakerscore=-0.373 if speaker=="Senator KERRY."
replace speakerscore=-0.254 if speaker=="Senator KLOBUCHAR."
replace speakerscore=-0.273 if speaker=="Senator KOHL."
replace speakerscore=0.563 if speaker=="Senator KYL."
replace speakerscore=-0.204 if speaker=="Senator LANDRIEU."
replace speakerscore=-0.403 if speaker=="Senator LAUTENBERG."
replace speakerscore=0.986 if speaker=="Senator LEE."
replace speakerscore=-0.411 if speaker=="Senator LEVIN."
replace speakerscore=-0.217 if speaker=="Senator LIEBERMAN."
replace speakerscore=-0.173 if speaker=="Senator LINCOLN."
replace speakerscore=0.392 if speaker=="Senator LOTT."
replace speakerscore=0.3 if speaker=="Senator LUGAR."
replace speakerscore=0.477 if speaker=="Senator MACK."
replace speakerscore=-0.069 if speaker=="Senator MANCHIN."
replace speakerscore=0.419 if speaker=="Senator MCCONNELL."
replace speakerscore=-0.372 if speaker=="Senator MENENDEZ."
replace speakerscore=-0.385 if speaker=="Senator MIKULSKI."
replace speakerscore=-0.321 if speaker=="Senator MOYNIHAN."
replace speakerscore=0.192 if speaker=="Senator MURKOWSKI."
replace speakerscore=-0.357 if speaker=="Senator MURRAY."
replace speakerscore=-0.035 if speaker=="Senator NELSON."
replace speakerscore=0.539 if speaker=="Senator NICKLES."
replace speakerscore=-0.201 if speaker=="Senator PRYOR."
replace speakerscore=-0.401 if speaker=="Senator REED."
replace speakerscore=-0.289 if speaker=="Senator REID."
replace speakerscore=-0.334 if speaker=="Senator ROCKEFELLER."
replace speakerscore=0.285 if speaker=="Senator ROTH."
replace speakerscore=0.579 if speaker=="Senator RUBIO."
replace speakerscore=-0.243 if speaker=="Senator SALAZAR."
replace speakerscore=0.345 if speaker=="Senator SANTORUM."
replace speakerscore=-0.448 if speaker=="Senator SARBANES."
replace speakerscore=-0.362 if speaker=="Senator SCHUMER."
replace speakerscore=0.545 if speaker=="Senator SESSIONS."
replace speakerscore=0.428 if speaker=="Senator SHELBY."
replace speakerscore=0.083 if speaker=="Senator SNOWE."
replace speakerscore=-0.335 if speaker=="Senator STABENOW."
replace speakerscore=0.377 if speaker=="Senator TALENT."
replace speakerscore=0.459 if speaker=="Senator THOMAS."
replace speakerscore=0.407 if speaker=="Senator THURMOND."
replace speakerscore=-0.272 if speaker=="Senator TORRICELLI."
replace speakerscore=0.505 if speaker=="Senator VITTER."
replace speakerscore=0.229 if speaker=="Senator VOINOVICH."
replace speakerscore=0.258 if speaker=="Senator WARNER." & year<2009
replace speakerscore=-0.222 if speaker=="Senator WARNER." & year>=2009
replace speakerscore=-0.183 if speaker=="Senator WEBB."
replace speakerscore=-0.457 if speaker=="Senator WHITEHOUSE."
replace speakerscore=0.338 if speaker=="Senator WICKER."
replace speakerscore=-0.324 if speaker=="Senator WYDEN."

replace speakerscore=-0.336 if speaker=="Representative BALDACCI."
replace speakerscore=0.395 if speaker=="Representative BONNER."
replace speakerscore=0.397 if speaker=="Representative CAMP."
replace speakerscore=0.271 if speaker=="Representative CAMPBELL."
replace speakerscore=0.235 if speaker=="Representative CASTLE."
replace speakerscore=-0.452 if speaker=="Representative CLYBURN."
replace speakerscore=0.548 if speaker=="Representative COX."
replace speakerscore=-0.441 if speaker=="Representative FORD." | speaker=="Mr. FORD"
replace speakerscore=0.04 if speaker=="Representative GILMAN."
replace speakerscore=0.419 if speaker=="Representative GRANGER."
replace speakerscore=0.43 if speaker=="Representative HART." | speaker=="Ms. HART"
replace speakerscore=0.142 if speaker=="Representative LEACH."
replace speakerscore=-0.104 if speaker=="Representative MATHESON."
replace speakerscore=0.351 if speaker=="Representative MCCOTTER."
replace speakerscore=0.263 if speaker=="Representative MURPHY."
replace speakerscore=0.363 if speaker=="Representative PICKERING."
replace speakerscore=-0.264 if speaker=="Representative POMEROY."
replace speakerscore=0.367 if speaker=="Representative REYNOLDS."
replace speakerscore=0.434 if speaker=="Representative ROGERS."
replace speakerscore=-0.456 if speaker=="Representative SCOTT."
replace speakerscore=0.672 if speaker=="Representative SENSENBRENNER."
replace speakerscore=0.405 if speaker=="Representative WHITE."
replace speakerscore=0.584 if speaker=="Representative WILSON."
replace speakerscore=-0.528 if speaker=="Mr. RANGEL."

replace speakerscore=0.285 if speaker=="Judge STAPLETON."
replace speakerscore=0.348 if speaker=="Judge KOZINSKI."

sort confirmationid

save "~\confirmationhearings1.dta", replace

use "~\qualitymeasurementmaster.dta"
sort confirmationid
merge m:m confirmationid using "~\confirmationhearings1.dta", generate (_merge6)
replace nomcs1=-0.317 if nomname=="Droney, Christopher"
replace nomcs1=-0.368 if nomname=="Kayatta, William"
replace nomcs1=-0.368 if nomname=="Halligan, Caitlin"
replace nomcs1=-0.2015 if nomname=="Thacker, Stephanie"
replace nomcs1=-0.368 if nomname=="Bacharach, Robert"
replace nomcs1=-0.452 if nompres=="Clinton" & nomcourtesy==0
replace nomcs1=-0.368 if nompres=="Obama" & nomcourtesy==0
replace nomcs1=0.729 if nompres=="W. Bush" & nomcourtesy==0
replace nomcs1=-0.321 if hearing=="AmbroThomas"
replace nomcs1=-0.372 if nomstate=="CA" & nomcourtesy==2
replace nomcs1=0.258 if hearing=="AgeeSteven"
replace nomcs1=0.3375 if hearing=="AllenClaude" | hearing=="HaynesWilliam" | hearing=="HaynesWilliam2" | hearing=="GregoryRoger" 
replace nomcs1=-0.2025 if hearing=="KeenanBarbara"
replace nomcs1=-0.3375 if hearing=="BarryMaryanne" | hearing=="FuentesJulio"
replace nomcs1=-0.3875 if hearing=="GreenawayJoseph" | hearing=="ShwartzPatty"
replace nomcs1=0.342 if hearing=="BentonWilliam" | hearing=="GruenderRaymond"
replace nomcs1=0.421 if hearing=="BoyleTerrence"
replace nomcs1=0.533 if hearing=="BybeeJay"
replace nomcs1=-0.3025 if hearing=="ByeKermit" | hearing=="KellyJohn"
replace nomcs1=-0.372 if hearing=="CampbellBonnie"
replace nomcs1=-0.2825 if hearing=="CarneySusan" | hearing=="ChatignyRobert"
replace nomcs1=-0.362 if hearing=="ChinDenny" | hearing=="LohierRaymond" | hearing=="LynchGerard"
replace nomcs1=-0.257 if hearing=="ChristenMorgan"
replace nomcs1=-0.411 if hearing=="ClayEric2"
replace nomcs1=0.343 if hearing=="CollotonSteven" | hearing=="MelloyMichael"
replace nomcs1=0.2585 if hearing=="CookDeborah" | hearing=="SuttonJeffrey"
replace nomcs1=-0.361 if hearing=="DavisAndre"
replace nomcs1=-0.213 if hearing=="DiazAlbert" | hearing=="WynnJames"
replace nomcs1=0.373 if hearing=="DuncanAllyson"
replace nomcs1=0.4375 if hearing=="ElrodJennifer" | hearing=="HaynesCatharina" | hearing=="OwenPriscilla2" | hearing=="PradoEdward"
replace nomcs1=0.203 if hearing=="FisherMichael" | hearing=="HardimanThomas" | hearing=="SmithBrooks" | hearing=="VanAntwerpenFranklin"
replace nomcs1=0.3935 if hearing=="GibbonsJulia"
replace nomcs1=0.586 if hearing=="GorsuchNeil"
replace nomcs1=-0.357 if hearing=="GouldRonald" | hearing=="McKeownMargaret" | hearing=="TallmanRichard"
replace nomcs1=-0.324 if hearing=="GraberSusan"
replace nomcs1=0.342 if hearing=="GruenderRaymond"
replace nomcs1=-0.174 if hearing=="HamiltonDavid1" | hearing=="HamiltonDavid2"
replace nomcs1=0.259 if hearing=="HartzHarris"
replace nomcs1=-0.204 if hearing=="HigginsonStephen"
replace nomcs1=0.6815 if hearing=="HolmesJerome"
replace nomcs1=0.5335 if hearing=="HowardJeffrey"
replace nomcs1=-0.232 if hearing=="HullFrank"
replace nomcs1=-0.196 if hearing=="JordanAdalberto" | hearing=="MarcusStanley"
replace nomcs1=-0.316 if hearing=="KingRobert"
replace nomcs1=0.359 if hearing=="McConnellMichael"
replace nomcs1=0.5005 if hearing=="MyersWilliam" | hearing=="MyersWilliam2" | hearing=="SmithRandy"
replace nomcs1=0.5055 if hearing=="OBrienTerrence"
replace nomcs1=0.4585 if hearing=="OwenPriscilla"
replace nomcs1=0.3415 if hearing=="PickeringCharles" | hearing=="PickeringCharles2" | hearing=="SouthwickLeslie" | hearing=="WallaceMichael"
replace nomcs1=-0.321 if hearing=="PoolerRosemary" | hearing=="SackRobert" | hearing=="SotomayorSonia" | hearing=="StraubChester"
replace nomcs1=0.4865 if hearing=="PryorWilliam"
replace nomcs1=-0.2735 if hearing=="RawlinsonJohnnie"
replace nomcs1=0.341 if hearing=="RileyWilliam"
replace nomcs1=0.474 if hearing=="RogersJohn"
replace nomcs1=0.407 if hearing=="SheddDennis"
replace nomcs1=0.429 if hearing=="SmithLavenski"
replace nomcs1=-0.429 if hearing=="ThompsonORogeriee"
replace nomcs1=0.3 if hearing=="TinderJohn"
replace nomcs1=-0.286 if hearing=="TraxlerWilliam"
replace nomcs1=0.4085 if hearing=="TymkovichTimothy"
replace nomcs1=-0.3205 if hearing=="VanaskieThomas"
replace nomcs1=-0.375 if hearing=="WilliamsAnne"
replace nomcs1=-0.25 if hearing=="WilsonCharles"


replace speakerscore=nomcs1 if speakerscore==. & Col1!=.
gen speakerdis=abs(speakerscore-nomcs1)
egen z1words=std(words)
gen logwords=log(words)

replace nomabaratingid=2 if nomname=="Colloton, Steven"
quietly destring, replace

generate nomcourtesy2=1 if nomcourtesy>=1
replace nomcourtesy2=0 if nomcourtesy==0
generate nomopposen=1 if nomcourtesy<=1
replace nomopposen=0 if nomcourtesy==2

gen majparty=1 if speakerscore>=0 & nomcongress==105 & _merge6 ==3 
replace majparty=1 if speakerscore>=0 & nomcongress==106 & _merge6==3
replace majparty=1 if speakerscore>=0 & nomcongress==108 & _merge6==3
replace majparty=1 if speakerscore>=0 & nomcongress==109 & _merge6==3
replace majparty=1 if speakerscore<=0 & nomcongress==107 & _merge6==3
replace majparty=1 if speakerscore<=0 & nomcongress==110 & _merge6==3
replace majparty=1 if speakerscore<=0 & nomcongress==111 & _merge6==3
replace majparty=1 if speakerscore<=0 & nomcongress==112 & _merge6==3
replace majparty=0 if majparty==. & _merge6==3

gen speakerrep=1 if speakerscore>=0 & _merge6==3
replace speakerrep=0 if speakerrep==. & _merge6==3
gen speakerdem=1 if speakerscore<=0 & _merge6==3
replace speakerdem=0 if speakerdem==. & _merge6==3
gen speakerdiff=1 if speakerrep==0 & nomprezparty==1 & _merge6==3
replace speakerdiff=1 if speakerrep==1 & nomprezparty==0 & _merge6==3
replace speakerdiff=0 if speakerdiff==. & _merge6==3
summarize speakerdiff if _merge6==3

gen nomcircuitid=1 if nomcircuit=="1st"
replace nomcircuitid=2 if nomcircuit=="2nd"
replace nomcircuitid=3 if nomcircuit=="3rd"
replace nomcircuitid=4 if nomcircuit=="4th"
replace nomcircuitid=5 if nomcircuit=="5th"
replace nomcircuitid=6 if nomcircuit=="6th"
replace nomcircuitid=7 if nomcircuit=="7th"
replace nomcircuitid=8 if nomcircuit=="8th"
replace nomcircuitid=9 if nomcircuit=="9th"
replace nomcircuitid=10 if nomcircuit=="10th"
replace nomcircuitid=11 if nomcircuit=="11th"
replace nomcircuitid=12 if nomcircuit=="DC"
replace nomcircuitid=13 if nomcircuit=="FC"
generate nomdccircuit=1 if nomcircuitid==12
replace nomdccircuit=0 if nomdccircuit==.
gen nomfccircuit=1 if nomcircuitid==13
replace nomfccircuit=0 if nomcircuitid!=13

rename date heardate
gen nomprezelect=1 if year==2000
replace nomprezelect=1 if year==2004
replace nomprezelect=1 if year==2008
replace nomprezelect=1 if year==2012
replace nomprezelect=0 if nomprezelect==.

egen circuityearid = concat(nomcircuitid year)

replace senatorspeaker=1 if speaker=="Judge KOZINSKI." | speaker=="Judge STAPLETON."
summarize exper_words if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomopposen if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomprezelect if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomdivgov if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize panelbalance1 if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomdccircuit if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomfccircuit if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomgender if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomage if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize majparty if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize presenter if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize presiding if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize hearing2 if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & senatorspeaker==1| housespeaker==1
summarize logwords if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomfedjudyrs if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomstjudyrs if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomprivcorppiyrs if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomgovattyrs if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomt14 if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomlawprof if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomfedclerk if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomallpol if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1
summarize nomrace if _merge6==3 & hearing!="WallaceMichael" & logwords!=. & senatorspeaker==1| housespeaker==1

merge m:m circuityearid using "~\Datasets to combine\partisan composition.dta", generate(_merge7)

summarize stdnompredababetter if _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1, detail
summarize stdnompredababetter if _merge6==3 & hearing!="WallaceMichael" & nomineespeaker==1, detail
sort words
summarize exper_words if _merge6==3 & hearing!="WallaceMichael"& logwords!=. & presenter==1
///Model I from Table 4
nbreg exper_words c.speakerdis##c.nomabaratingid i.nomopposen  i.nomprezelect i.nomdivgov##c.panelbalance1 i.nomdccircuit i.nomfccircuit i.nomrace i.nomgender c.nomage##c.nomage i.majparty i.presenter i.presiding i.hearing2 c.logwords   if _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1, vce(cl heardate)
estat ic
margins, at(speakerdis=(0(.1)1.4) nomabaratingid=(1 5)) post
///Top panel from Figure 2
marginsplot, x(speakerdis) recast(line) recastci(rarea)
///Model II from Table 4
nbreg exper_words c.speakerdis##c.stdnompredababetter i.nomopposen  i.nomprezelect i.nomdivgov##c.panelbalance1 i.nomdccircuit i.nomfccircuit i.nomrace i.nomgender c.nomage##c.nomage i.majparty i.presenter i.presiding i.hearing2 c.logwords if _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1, vce(cl heardate)
estat ic
margins, at(speakerdis=(0(.1)1.4) stdnompredababetter=(-1 2)) post
test _b[30._at] = _b[29._at]
test _b[13._at] = _b[14._at]
test _b[15._at] = _b[16._at]
test _b[17._at] = _b[18._at]
test _b[25._at] = _b[26._at]
test _b[2._at] = _b[58._at]
test _b[2._at] = _b[1._at]
test _b[2._at] = _b[30._at]
test _b[1._at] = _b[29._at]
test _b[37._at] = _b[38._at]
count if speakerdis>.60 & _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1
count if speakerdis>.90 & _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1

count if _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1
///Model III from Table 4
nbreg exper_words c.speakerdis##c.nomfedjudyrs c.nomstjudyrs c.speakerdis#c.nomstjudyrs c.nomprivcorppiyrs c.speakerdis#c.nomprivcorppiyrs c.nomgovattyrs c.speakerdis#c.nomgovattyrs i.nomlawprof c.speakerdis#i.nomlawprof i.nomfedclerk c.speakerdis#i.nomfedclerk i.nomt14 c.speakerdis#i.nomt14 i.nomallpol c.speakerdis#i.nomallpol i.nomopposen  i.nomprezelect i.nomdivgov##c.panelbalance1 i.nomdccircuit i.nomfccircuit i.nomrace i.nomgender c.nomage##c.nomage i.majparty i.presenter i.presiding i.hearing2 c.logwords  if _merge6==3 & hearing!="WallaceMichael" & senatorspeaker==1 | housespeaker==1, vce(cl heardate)
estat ic